package com.gary.spider;

import java.util.ArrayList;
import java.util.HashSet;

import javax.swing.text.html.HTMLDocument.Iterator;

public class Dispatcher {

	private static ArrayList<String> urls = new ArrayList<String>();
	private static ArrayList<String> visitedURLs = new ArrayList<String>();
	private static ArrayList<String> unvisitedURLs = new ArrayList<String>();

	
	private static Dispatcher dispatcher;
	private Dispatcher() {   
		this.urls.add("http://www.163.com");
	}    
	
	public static synchronized Dispatcher getDispatcher(){
		if(null==dispatcher){
			dispatcher=new Dispatcher();
		}
		return dispatcher;
	}
	
	public synchronized String getURL()		
	{
		while(urls.isEmpty()){ 
			try{ 
				wait(); 
			} catch (InterruptedException e) { 
				e.printStackTrace(); 
			} 
		}
		
		this.notify(); 
		String String = urls.get(0);
		visitedURLs.add(String);
		urls.remove(String);
		
	    return String; 
	}

	public synchronized void insert(String String)
	{
		if(!urls.contains(String) && !visitedURLs.contains(String))
			urls.add(String);
	}

	public synchronized void insert(ArrayList<String> analyzedURL)
	{
		for(String String : analyzedURL)
		{
			if(!urls.contains(String) && !visitedURLs.contains(String))
			urls.add(String);
		}
	}
    
}

